package com.example.administrator.szb.activity.luyan;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.GZXYActivity;
import com.example.administrator.szb.activity.Success2Activity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.IosDiaolog;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.view.AddressUtils;
import com.example.administrator.szb.view.TimePickUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LuYanNewActivity extends MVPBaseActivity implements View.OnClickListener{

    private TextView bar_title;
    private EditText et_title;
    private EditText et_company;
    private LinearLayout ll_type;
    private TextView tv_type;
    private LinearLayout ll_date;
    private TextView tv_date;
    private LinearLayout ll_time;
    private TextView tv_time;
    private LinearLayout ll_end;
    private TextView tv_end_time;
    private LinearLayout ll_address;
    private TextView tv_address;
    private EditText et_address;
    private EditText td_name;
    private EditText et_phone;
    private EditText tv_content;
    private Button send_btn;
    private TextView sqxm_text_bmxy;
    AddressUtils addressUtils;
    private int select = -1;
    private int city_id;
    private List<LuYanTypes> luYanTypes = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lu_yan_new);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        addressUtils = new AddressUtils(context);
        et_title = (EditText) findViewById(R.id.et_title);
        et_company = (EditText) findViewById(R.id.et_company);
        ll_type = (LinearLayout) findViewById(R.id.ll_type);
        tv_type = (TextView) findViewById(R.id.tv_type);
        ll_date = (LinearLayout) findViewById(R.id.ll_date);
        tv_date = (TextView) findViewById(R.id.tv_date);
        ll_time = (LinearLayout) findViewById(R.id.ll_time);
        tv_time = (TextView) findViewById(R.id.tv_time);
        ll_end = (LinearLayout) findViewById(R.id.ll_end);
        tv_end_time = (TextView) findViewById(R.id.tv_end_time);
        ll_address = (LinearLayout) findViewById(R.id.ll_address);
        tv_address = (TextView) findViewById(R.id.tv_address);
        et_address = (EditText) findViewById(R.id.et_address);
        td_name = (EditText) findViewById(R.id.td_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_content = (EditText) findViewById(R.id.tv_content);
        setEditEvent(tv_content,R.id.tv_content);
        send_btn = (Button) findViewById(R.id.send_btn);
        sqxm_text_bmxy = (TextView) findViewById(R.id.sqxm_text_bmxy);
        tv_address = findViewById(R.id.tv_address);
        ll_address = findViewById(R.id.ll_address);
        ll_address.setOnClickListener(this);
        ll_type.setOnClickListener(this);
        ll_date.setOnClickListener(this);
        ll_time.setOnClickListener(this);
        ll_end.setOnClickListener(this);

        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("路演调研");
        send_btn = (Button) findViewById(R.id.send_btn);
        TextView sqxm_text_bmxy = findViewById(R.id.sqxm_text_bmxy);
        sqxm_text_bmxy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_web = new Intent(context,GZXYActivity.class);//额，
                intent_web.putExtra("title","保密承诺");
                intent_web.putExtra("id",2);
                startActivity(intent_web);
            }
        });
    }

    @Override
    protected void initEvent() {
        et_phone.setText(SPUtils.getDatasString(SPUtils.USER_PHONE));
        addressUtils.setListener(new AddressUtils.OnAddressSelectListener() {
            @Override
            public void onSelect(String address, int id) {
                tv_address.setText(address);
                city_id=id;
            }
        });
        send_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckInfoUtils.check(activity, iosDiaolog, send_btn, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        if (canDo()){
                            doFabu();
                        }
                    }
                });


            }
        });
    }

    @Override
    protected void initData() {
        getTypes();
    }
    private void doFabu(){
        send_btn.setEnabled(false);
        DialogUtil.showIsoProgressbar(context,"发布中");
        HttpUtils.post(activity,URLAddress.LUYAN_ADD, getMap(), new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                send_btn.setEnabled(true);
                DialogUtil.showToast(context,str);
                Intent intent = new Intent(context, Success2Activity.class);
                intent.putExtra("titles","发布成功");
                intent.putExtra("content","路演发布成功，客服经理将在24小时内与您联系");
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                send_btn.setEnabled(true);
            }
        });
    }
    @Override
    public void onClick(View v) {
        /*
        ll_type.setOnClickListener(this);
        ll_date.setOnClickListener(this);
        ll_time.setOnClickListener(this);
        ll_end.setOnClickListener(this);*/
        switch (v.getId()){
            case R.id.ll_address:
                addressUtils.show();
                break;
            case R.id.ll_type:
                if (luYanTypes.size()>0){
                    List<String> l = new ArrayList<>();
                    for (int i=0;i<luYanTypes.size();i++){
                        l.add(luYanTypes.get(i).getName());
                    }
                    TimePickUtil.show(context, l, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            select=luYanTypes.get(options1).getType();
                            tv_type.setText(luYanTypes.get(options1).getName());
                        }
                    });
                }
                break;
            case R.id.ll_time:
                TimePickUtil.initCustomTimePicker2(context, 1, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tv_time.setText(TimePickUtil.getTime1(date));
                    }
                });
                break;
            case R.id.ll_date:
                TimePickUtil.initCustomTimePicker2(context,0, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tv_date.setText(TimePickUtil.getTime(date));
                    }
                });
                break;
            case R.id.ll_end:
                TimePickUtil.initCustomTimePicker2(context,0, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tv_end_time.setText(TimePickUtil.getTime(date));
                    }
                });
                break;
        }
    }
    private boolean canDo(){
        boolean can = true;
        String str = "";
        if (TextUtils.isEmpty(et_title.getText().toString().trim())){
            str="请输入主题";
            can=false;
        }else if (TextUtils.isEmpty(et_company.getText().toString().trim())){
            str="请输入公司名称";
            can=false;
        }else if (TextUtils.isEmpty(tv_type.getText().toString().trim())){
            str="请选择类型";
            can=false;
        }else if (TextUtils.isEmpty(tv_date.getText().toString().trim())){
            str="请选择举办日期";
            can=false;
        }else if (TextUtils.isEmpty(tv_time.getText().toString().trim())){
            str="请选择开始时间";
            can=false;
        }else if (TextUtils.isEmpty(tv_end_time.getText().toString().trim())){
            str="请选择截止日";
            can=false;
        }/*else if (TextUtils.isEmpty(tv_address.getText().toString().trim())){
            str="请选择地址";
            can=false;
        }else if (TextUtils.isEmpty(et_address.getText().toString().trim())){
            str="请输入详细地址";
            can=false;
        }*/else if (TextUtils.isEmpty(td_name.getText().toString().trim())){
            str="请输入联系人";
            can=false;
        }else if (!Contants.isMobileNO(et_phone.getText().toString().trim())){
            str="请输入正确的联系方式";
            can=false;
        }
        if (!can)
        DialogUtil.showToast(context,str);
        return can;
    }
    @Override
    public void onFinish(View view) {
        back();
    }

    @Override
    public void onBackPressed() {
        back();
    }

    private void back(){
        iosDiaolog.setMsgs("您的项目尚未提交,是否选择离开?");
        iosDiaolog.setLeftButton("离开");
        iosDiaolog.setRightButton("继续填写");
        iosDiaolog.setLeftClick(new IosDiaolog.OnIosDialogClickListener() {
            @Override
            public void onClick(String str, PopupWindow popupWindows) {
                finish();
            }
        });
        iosDiaolog.show(ll_time);
    }
    private Map<String,Object> getMap(){
        /*uid=383(用户id)&type=1(路演类型)&title=aaa&company=aaa&start_time=2018-08-01 12:38:00
&end_time=2018-08-01 12:38:00&address=小区&city_id=310104(地址的编号)&contacts=石云鹤&tel=18337149202&extras=备注*/
        Map<String,Object> map = new HashMap<>();
        map.put("uid",SPUtils.getUserId());
        map.put("type",select);
        map.put("title",et_title.getText().toString().trim());
        map.put("company",et_company.getText().toString().trim());
        map.put("date",tv_date.getText().toString());
        map.put("time",tv_time.getText().toString());
        map.put("end_time",tv_end_time.getText().toString());
        map.put("address",tv_address.getText().toString());
        map.put("city_id",city_id);
        map.put("contacts",td_name.getText().toString().trim());
        map.put("tel",et_phone.getText().toString().trim());
        map.put("extras",tv_content.getText().toString().trim());
        return map;
    }
    private void getTypes(){
        Map<String,Object> map = new HashMap<>();
        HttpUtils.post(activity,URLAddress.LUYAN_GETTYPES, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                luYanTypes.clear();
                luYanTypes.addAll(JSON.parseArray(str,LuYanTypes.class));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
   static class LuYanTypes{

        /**
         * id : 1
         * type : 1
         * name : 调研
         * enable : 0
         * create_time : 2018-01-26 13:36:00
         * sorts : 1
         * delete_time : null
         * mark : null
         */

        private int id;
        private int type;
        private String name;
        private int enable;
        private String create_time;
        private int sorts;
        private Object delete_time;
        private Object mark;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getEnable() {
            return enable;
        }

        public void setEnable(int enable) {
            this.enable = enable;
        }

        public String getCreate_time() {
            return create_time;
        }

        public void setCreate_time(String create_time) {
            this.create_time = create_time;
        }

        public int getSorts() {
            return sorts;
        }

        public void setSorts(int sorts) {
            this.sorts = sorts;
        }

        public Object getDelete_time() {
            return delete_time;
        }

        public void setDelete_time(Object delete_time) {
            this.delete_time = delete_time;
        }

        public Object getMark() {
            return mark;
        }

        public void setMark(Object mark) {
            this.mark = mark;
        }
    }
}
